package com.wuchao.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.github.pagehelper.PageInfo;
import com.wuchao.entity.User;
import com.wuchao.service.UserService;
import com.wuchao.util.BaseUserController;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2022/7/7 16:23
 * @Version 1.0
 */
//注解为mvc
 @org.springframework.stereotype.Controller
/* @RestController*/
 /*@RequestMapping("user")*/
public class Controller  {//extends BaseUserController
    @Resource
    private UserService userService;//自动注入mybatis+spring
    @RequestMapping("main")//1
    public String main(){
        return "main";
    }

    @RequestMapping("load")//去load
    public String load(){
        return "load";
    }
    //异步模糊查询--返回值只能String json
    @RequestMapping(name = "findAjax")/*,produces={"application/json;charset=UTF-8"}*/
    @ResponseBody//异步
    //@CrossOrigin//跨域
    public Object findAjax(@RequestParam(value = "name",required = false,
            defaultValue = "") String name){
        List<User> list = userService.findUserByName(name);

       return JSON.toJSONString(list);//返回json
    }

//去分页界面
@RequestMapping("page")//1
public String page(){
    return "page";
}
//分页的模糊查询
    @RequestMapping(value = "findUserByPage")
    public ModelAndView findUserByPage(@RequestParam(name = "currentPage",required = false,defaultValue = "1") int currentPage,
                                       @RequestParam(name = "pageSize",required = false,defaultValue = "2") int pageSize,
                                       @RequestParam(name = "name",required = false,defaultValue = "") String name ){
        PageInfo<User> pageInfo = userService.findUserByPage(currentPage, pageSize, name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page");
        modelAndView.addObject("pageInfo",pageInfo);
        return modelAndView;
    }


 //异步查询
    @RequestMapping(value = "find")
    @ResponseBody
    public  Object find(
            @RequestParam(name = "name",required = false,defaultValue = "")
                    String name){//model在括号
        List<User> list = userService.findUserByName(name);
        return JSON.toJSONString(list);
    }
   /*//异步查询
   @RequestMapping(value = "find",produces={"application/json;charset=UTF-8"})
   @ResponseBody
   public  Object find(
           @RequestParam(name = "name",required = false,defaultValue = "")
                   String name){//model在括号
       List<User> list = userService.findUserByName(name);
       return JSON.toJSONString(list);
   }*/
  /*  @ResponseBody*/
    @RequestMapping("findUserByName")//方法名不能重复页也支持重载//2
    public ModelAndView findUserByName(String name){
        List<User> list = userService.findUserByName(name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("main");
        return modelAndView;
    }
    //3rest风格
    @RequestMapping(value = "findUserByName2",method = RequestMethod.GET)
    public  ModelAndView findUserByName2(
            @RequestParam(name = "name",required = false,defaultValue = "")
                    String name){//model在括号
        List<User> list = userService.findUserByName(name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("main");
        return modelAndView;
    }
    //5
    @RequestMapping("deleteUserByid")
    public  ModelAndView deleteUserByid(@RequestParam(name = "id",required = true)
                    int id){//model在括号
        int i = userService.deleteByPrimaryKey(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("isdelete",i);
        modelAndView.setViewName("main");
        return modelAndView;
    }
    //4
    @RequestMapping("findUserByName1")
    public  String findUserByName1(String name, Model model){//model在括号
        List<User> list = userService.findUserByName(name);
        model.addAttribute("list",list);
        return "main";
    }
 /*   @RequestMapping("main1")//调用方法名
    public String main1(){//返回值y1只有2个 String ModelAndView
        return "main";
    }*/
}
